package org.springframework.xml.xpath.proxy.conversion.base;

import java.beans.PropertyEditor;

/**
 * This class is used to adapt any PropertyEditor for use in the Conversion mechanisms of the XpathProxyFactory
 *
 * @author Ray Krueger
 */
public class PropertyEditorConverterAdapter<T> extends AbstractInputStringConverter<T> {

    private final Class<T> supportedType;
    private final PropertyEditor propertyEditor;

    public PropertyEditorConverterAdapter(Class<T> supportedType, PropertyEditor propertyEditor) {
        this.supportedType = supportedType;
        this.propertyEditor = propertyEditor;
    }

    @SuppressWarnings("unchecked")
    protected T convertString(String input) {
        synchronized (propertyEditor) {
            propertyEditor.setAsText(input);
            return (T) propertyEditor.getValue();
        }
    }

    protected Class<T> getSupportedType() {
        return supportedType;
    }
}
